package com.sys.group.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sys.common.vo.Result;
import com.sys.group.entity.User;
import com.sys.group.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gyf
 * @since 2023-05-15
 */
@Api(tags = {"用户管理接口"})
@RestController
@RequestMapping("/user")
public class UserController {
    /*
    * 装配service对象
    * */
    @Autowired
    private IUserService userService;


    // 查询所有用户
    @ApiOperation("查询所有用户接口")
    @GetMapping("/all")
    public Result<List<User>> getAllUser() {
        List<User> list = userService.list();
        return Result.success(list, "查询成功");
    }

    /*
    * 登录
    * 参数User传入是json字符串的格式，加上@RequestBody转换
    * */
    @ApiOperation("登录接口")
    @PostMapping("/login")
    public Result<Map<String, Object>> login(@RequestBody User user) {
        Map<String, Object> data = userService.login(user);

        if (data != null) return Result.success(data);
        return Result.fail(20002, "用户名或密码错误");
    }

    @ApiOperation("用户详细信息接口")
    @GetMapping("/info")
    public Result<Map<String, Object>> getUserInfo(@RequestParam("token") String token) {
        // 根据token获取用户信息 redis
        Map<String, Object> data = userService.getUserInfo(token);

        if (data != null) return Result.success(data);
        return Result.fail(20003, "登录信息无效，请重新登陆！");
    }

    @ApiOperation("用户注销接口")
    @PostMapping("/logout")
    public Result<?> logout(@RequestHeader("Token") String token) {
        userService.logout(token);
        return Result.success("退出成功！");
    }

    @ApiOperation("用户列表接口")
    @GetMapping("/list")
    public Result<Map<String, Object>> getUserList(
            @RequestParam(value = "username", required = false) String username,
            @RequestParam(value = "phone", required = false) String phone,
            @RequestParam(value = "email", required = false) String email,
            @RequestParam("pageNo") Long pageNo,
            @RequestParam("pageSize") Long pageSize
    ) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasLength(username), User::getUsername, username);
        wrapper.like(StringUtils.hasLength(phone), User::getPhone, phone);
        wrapper.like(StringUtils.hasLength(email), User::getEmail, email);
        wrapper.orderByAsc(User::getId);

        Page<User> page = new Page<>(pageNo, pageSize);
        userService.page(page, wrapper);

        Map<String, Object> data = new HashMap<>();
        data.put("total", page.getTotal());
        data.put("userList", page.getRecords());

        return Result.success(data);
    }

    @ApiOperation("添加用户接口")
    @PostMapping
    public Result<?> addUser(@RequestBody User user) {
        userService.addUser(user);
        return Result.success("新增用户成功！");
    }

    @ApiOperation("更新用户接口")
    @PutMapping
    public Result<?> updateUser(@RequestBody User user) {
        user.setPassword(null);
        userService.updateUser(user);
        return Result.success("修改用户成功！");
    }

    @ApiOperation("用户更新个人信息接口")
    @PutMapping("/myinfo")
    public Result<?> updateMyInfo(@RequestBody User user) {
        if (StringUtils.hasLength(user.getPassword())) {
            System.out.println("updateMyInfo: " + user );
            userService.updateMyInfo(user);
        } else {
            user.setPassword(null);
            userService.updateUser(user);
        }
        return Result.success("修改成功！");
    }

    @ApiOperation("根据id获取用户信息接口")
    @GetMapping("/{id}")
    public Result<User> getUserById(@PathVariable("id") Integer id) {
        User user = userService.getUserById(id);
        return Result.success(user);
    }

    @ApiOperation("删除用户接口")
    @DeleteMapping("/{id}")
    public Result<User> deleteUserById(@PathVariable("id") Integer id) {
        userService.deleteUserById(id);
        return Result.success("删除用户成功！");
    }
}
